package com.example.activity;

import android.Manifest;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.example.R;
import com.example.adapter.CustomPageAdapter;
import com.example.fragment.BonusFragment;
import com.example.fragment.IndexFragment;
import com.example.fragment.MineFragment;
import com.example.widget.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * MainActivity
 */
public class MainActivity extends AppCompatActivity {
    @BindView(R.id.view_page)
    NoScrollViewPager viewPager;
    @BindView(R.id.img_index)
    ImageView imgIndex;
    @BindView(R.id.img_bonus)
    ImageView imgBonus;
    @BindView(R.id.img_mine)
    ImageView imgMine;
    @BindView(R.id.tv_index)
    TextView tvIndex;
    @BindView(R.id.tv_bonus)
    TextView tvBonus;
    @BindView(R.id.tv_mine)
    TextView tvMine;

    private List<Fragment> fragments;
    private CustomPageAdapter pageAdapter;
    private long exitTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        fragments = new ArrayList<>();
        fragments.add(new IndexFragment());
        fragments.add(new BonusFragment());
        fragments.add(new MineFragment());

        pageAdapter = new CustomPageAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(pageAdapter);
        viewPager.setOffscreenPageLimit(2);

        checkPermission();
    }


    /**
     * @param index
     */
    private void initNavigation(int index){
        switch(index){
            case 0:
                imgIndex.setImageResource(R.mipmap.icon_index_selected);
                imgBonus.setImageResource(R.mipmap.icon_bonus);
                imgMine.setImageResource(R.mipmap.icon_mine);

                tvIndex.setTextColor(Color.parseColor("#FF5112"));
                tvBonus.setTextColor(Color.parseColor("#282828"));
                tvMine.setTextColor(Color.parseColor("#282828"));
                break;
            case 1:
                imgIndex.setImageResource(R.mipmap.icon_index);
                imgBonus.setImageResource(R.mipmap.icon_bonus_selected);
                imgMine.setImageResource(R.mipmap.icon_mine);

                tvIndex.setTextColor(Color.parseColor("#282828"));
                tvBonus.setTextColor(Color.parseColor("#FF5112"));
                tvMine.setTextColor(Color.parseColor("#282828"));
                break;
            case 2:
                imgIndex.setImageResource(R.mipmap.icon_index);
                imgBonus.setImageResource(R.mipmap.icon_bonus);
                imgMine.setImageResource(R.mipmap.icon_mine_selected);

                tvIndex.setTextColor(Color.parseColor("#282828"));
                tvBonus.setTextColor(Color.parseColor("#282828"));
                tvMine.setTextColor(Color.parseColor("#FF5112"));
                break;
        }
    }

    /**
     * 权限检查
     */
    private void checkPermission(){
        requestPermissions(new String[]{
                Manifest.permission.CAMERA,
                Manifest.permission.WAKE_LOCK,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    }

    @OnClick({R.id.lay_index,R.id.lay_bonus,R.id.lay_mine})
    public void onClickView(View view){
        switch(view.getId()){
            case R.id.lay_index:
                initNavigation(0);
                viewPager.setCurrentItem(0);
                break;
            case R.id.lay_bonus:
                initNavigation(1);
                viewPager.setCurrentItem(1);
                break;
            case R.id.lay_mine:
                initNavigation(2);
                viewPager.setCurrentItem(2);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            if(System.currentTimeMillis()-exitTime>2000){
                Toast.makeText(this,"再按一次退出程序",Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            }else{
                System.exit(0);
            }

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}